/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例使用 printf 打印字符到 UART，并通过串口转USB芯片打印到PC
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：短接J13
  *                    断开J12
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/
typedef struct
{
    uint8_t Day;
    uint8_t Month;
    uint16_t Year;
} CompileDateTypeDef;

CompileDateTypeDef gCompileDate;

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void Get_Compile_Date(CompileDateTypeDef *CompileDate);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 初始化系统时钟
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化SysTick
    HAL_InitTick(48000000);

    // 板载UART及端口初始化
    Bsp_Uart_Init(48000000, 115200);

    // 获取编译时间
    Get_Compile_Date(&gCompileDate);

    //------------------------------------------------------------
    // 每300ms从TXD管脚(PB02)发送当前systick的ms计数值
    while (1)
    {
        uint32_t tmp32 = HAL_GetTick();
        printf("TickCnt(Dec) = %d\t\tTickCnt(Hex) = 0x%X\r\n", tmp32, tmp32);

        printf("%04d-%02d-%02d\r\n", gCompileDate.Year, gCompileDate.Month, gCompileDate.Day);

        HAL_Delay(300);
    }
}


#include "stdlib.h"
static void Get_Compile_Date(CompileDateTypeDef *CompileDate)
{
    const char *pMonth[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    const char Date[12] = __DATE__;                                   // 取编译时间
    uint8_t i;

    for (i = 0; i < 12; i++)
    {
        if (memcmp(Date, pMonth[i], 3) == 0)
        {
            CompileDate->Month = i + 1;
            break;
        }
    }
    CompileDate->Year = (uint16_t)atoi(Date + 7);                     // Date[9]为２位年份，Date[7]为完整年份
    CompileDate->Day = (uint8_t)atoi(Date + 4);
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
